const fs = require("fs");

class CreateSetting {
  constructor() {
    this._data = null;
    this.readErr = null;
    this.start();
  }

  get data() {
    return this._data;
  }

  set data(x) {
    this._data = x;
  }
  start() {
    this.read();
  }
  set readErr(x) {
    this._err = x;
  }
  get readErr() {
    return this._err;
  }

  read() {
    try {
      const configContent = fs.readFileSync("api.config.js", "utf-8");
      const config = eval(configContent);
      this.data = config;
    } catch (e) {
      this.readErr = e;
      console.log("文件不存在", e);
      this.write();
    }
  }
  write() {
    fs.writeFileSync("api.config.js", this.createFileStream(), (err) => {
      if (err) {
        return console.log(err);
      }
      console.log("文件写入成功");
    });
  }

  createFileStream() {
    const res = `
        module.exports = {
                /**
                 * swagger api 地址
                 * **/
                apiUrl: "",
                // request url 过滤
                requestUrlFilter: [],
                /**
                 * request 服务来源
                 * **/
                apiHead: import { request } from '@/services/request',
                /**
                 * 一些type声明
                 * **/
                apiClientType: ``,
                /**
                 * 对应key的类型替换
                 * **/
                 typeEnum: {
                  integer: "string",
                  int32: "string",
                  int64: "string",
                },
                /**
                 * 生成文件夹路径
                 * **/
                fileFolder: "api/",
              
            }
        `;
    return res;
  }
}
module.exports = new CreateSetting();
